package com.sgb.kjwl.view.ui.fragment.self;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityTruckApplyBinding;
import com.sgb.kjwl.model.entity.self.TruckApplyEntity;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.activity.self.logistics.OfferOwnerDetailsActivity;
import com.sgb.kjwl.view.ui.adapter.self.offer.TruckApplyAdapter;
import com.sgb.kjwl.viewmodel.self.offer.TruckApplyModel;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Describe: 原货车报名2.1.2更改如下
 * Author:  熊丽
 * Date:    2021/9/23
 */
public class TruckApplyFragment extends AppLazyFragment {
    private ActivityTruckApplyBinding binding;
    TruckApplyModel viewModel;
    TruckApplyAdapter adapter;
    private boolean isGeneralGoods;

    public static TruckApplyFragment newInstance(boolean isGeneralGoods) {
        TruckApplyFragment fragment = new TruckApplyFragment();
        Bundle args = new Bundle();
        args.putBoolean(RouterKey.BOOLEAN, isGeneralGoods);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.activity_truck_apply, container, false);
        binding.setLifecycleOwner(this);
        return binding.getRoot();
    }
    @Override
    protected void initView() {
        initIntentParams();
        binding.topBar.titleView.setText("货车报名");
        binding.topBar.topLayout.setVisibility(View.GONE);
        binding.topBar.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        binding.topBar.ivBack.setOnClickListener(v -> getActivity().finish());
        viewModel = new ViewModelProvider(this).get(TruckApplyModel.class);
        if (adapter == null)
            adapter = new TruckApplyAdapter(getActivity(), null);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
       /* DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divide));
        binding.recyclerView.addItemDecoration(divider);*/
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(false);
        binding.recyclerView.setPullRefreshEnabled(false);
        binding.smartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.getTrucks(getActivity(), false,isGeneralGoods);
                binding.smartRefresh.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getTrucks(getActivity(), true,isGeneralGoods);
                binding.smartRefresh.finishRefresh();
            }
        });

        adapter.setOnItemClick(new AdapterOnItemClick<TruckApplyEntity>() {
            @Override
            public void onItemClick(TruckApplyEntity truckApplyEntity, int position) {
                // 跳转报价车主的报价详情
                OfferOwnerDetailsActivity.start(getActivity(), truckApplyEntity.getGoodsOfferNo(), true);
            }
        });

        viewModel.refreshLiveData.observe(this, truckApplyEntities -> showData(truckApplyEntities));
        viewModel.loadMoreLiveData.observe(this, truckApplyEntities -> loadMoreData(truckApplyEntities));
    }

    @Override
    protected void onLazyLoad() {
    }
    private void initIntentParams() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            isGeneralGoods = getArguments().getBoolean(RouterKey.BOOLEAN, true);
        }
    }
    /**
     * 展示第一页数据
     *
     * @param list
     */
    public void showData(List<TruckApplyEntity> list) {
        if (list == null)
            return;
        if (list.size() == 0) {
            binding.emptyView.setVisibility(View.VISIBLE);
        } else {
            binding.emptyView.setVisibility(View.GONE);
            if (isGeneralGoods){
                adapter.setIsisGeneralGoods(isGeneralGoods);
            }
            adapter.setDatas(list);
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        viewModel.getTrucks(getActivity(), true,isGeneralGoods);
    }
    /**
     * 加载更多数据
     *
     * @param list
     */
    public void loadMoreData(List<TruckApplyEntity> list) {
        if (list == null)
            return;
        if (list.size() > 0) {
            if (isGeneralGoods){
                adapter.setIsisGeneralGoods(isGeneralGoods);
            }
            adapter.addDatas(list);
        }
    }
}
